package com.hero.updown;

import android.content.Context;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.io.File;

public class UpUtils {

    private Context context;

    public UpUtils(Context context){
        this.context = context;
    }


    public void upload(String url,String path) {
        upload(url,path,null);
    }

    public void upload(String url,String path,StatusCallback callback) {

        ProgressBarDialog dialog = new ProgressBarDialog(context,callback,0);

        OkGo.<String>post(url)
                .tag(this)
                .params("uid", 1)
                .params("image", new File(path))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        if (dialog != null){
                            dialog.show();
                            dialog.setText1(path);
                        }
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("111",response.code()+"--------------------"+response.body());
                        if (dialog != null){
                            dialog.setText2(response.body());
                            dialog.setClose();
                        }
                        Toast.makeText(context,"上传成功！",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (dialog != null){
                            dialog.setClose();
                        }
                        Toast.makeText(context,"上传失败！",Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void uploadProgress(Progress progress) {
                        if (dialog != null){
                            dialog.setProgress(progress.fraction);
                        }
//                        super.uploadProgress(progress);

                    }
                });
    }
}
